import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.elm.R
import com.example.elm.Messages.MessageModel

class ChatAdapter(private val messageList: List<MessageModel>) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view: View = LayoutInflater.from(parent.context).inflate(R.layout.message_item, parent, false)
        return MessageViewHolder(view)
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val message = messageList[position]
        if (holder is MessageViewHolder) {
            // 显示消息内容
            holder.leftMessage.text = message.left_content
            holder.leftName.text = message.receiverId
            holder.leftImage.setImageResource(message.receiverHead)
            holder.rightMessage.text = message.right_content
            holder.rightName.text = message.senderId
        }
    }

    override fun getItemCount(): Int = messageList.size

    inner class MessageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val leftMessage: TextView = itemView.findViewById(R.id.left_content)
        val leftName: TextView = itemView.findViewById(R.id.left_name)
        val leftImage: ImageView = itemView.findViewById(R.id.left_image)
        val rightMessage: TextView = itemView.findViewById(R.id.right_content)
        val rightName: TextView = itemView.findViewById(R.id.right_name)
    }
}
